package com.pactera.jep.service.yapin.eval.value.controller;

import com.pactera.jep.service.yapin.eval.value.model.EvalProcess;
import com.pactera.jep.service.yapin.eval.value.model.EvalProcessCltDetails;
import com.pactera.jep.service.yapin.eval.value.service.EvalProcessCltDetailsService;
import com.pactera.jep.service.yapin.eval.value.service.EvalProcessService;
import com.pactera.jep.service.yapin.eval.value.vo.AmtConfReasonVo;
import com.pactera.jep.service.yapin.eval.value.vo.AssessedEvalVo;
import com.pactera.jep.service.yapin.eval.value.vo.ExtlEvalVo;
import com.pactera.jep.web.entity.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 *
 * @Author YouName
 * @Date 2020-7-20 10:33:35
 */
@RestController
@RequestMapping("/firstEvaluate")
@Api(description = "首次评估")
public class FirstEvaluateContronller {

    @Autowired
    private EvalProcessCltDetailsService evalProcessCltDetailsService;

    @Autowired
    private EvalProcessService evalProcessService;

    @PostMapping("/createExtlEval")
    @ResponseBody
    @ApiOperation("新建或更新 外部评估")
    public Result createOrUpdate(@RequestBody ExtlEvalVo extlEvalVo){

        try {
            Assert.hasLength(extlEvalVo.getEvalProcessId(),"评估流程明细Id为空");
            Assert.hasLength(extlEvalVo.getEvalProcessNo(),"流程编号为空");
            Assert.hasLength(extlEvalVo.getCltNo(),"押品编号为空");
            Assert.hasLength(extlEvalVo.getCltTpCd(),"押品类型为空");
            Assert.hasLength(extlEvalVo.getExterEvalOrgNo(),"外部评估机构为空");
            Assert.hasLength(extlEvalVo.getExterEvalMtdCd(),"外部评估方法为空");
            Assert.hasLength(extlEvalVo.getExterEvalVal().toString(),"外部评估价值为空");
            Assert.hasLength(extlEvalVo.getExterCcyCd(),"外部评估币种为空");
            Assert.hasLength(extlEvalVo.getExterEvalDt().toString(),"外部评估日期为空");
            Assert.hasLength(extlEvalVo.getExterEvalOrgNm().toString(),"外部机构名称为空");

            evalProcessCltDetailsService.createOrUpdate(extlEvalVo);
            return Result.success("操作成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.error(400,"系统繁忙，请稍后再试");
    }
    @GetMapping("/getExtlEval")
    @ResponseBody
    @ApiOperation("获取外部评估")
    public Result getExtlEval(@RequestParam("id") String id){
        try {
            EvalProcessCltDetails extlEval = evalProcessCltDetailsService.get(id);
            if(extlEval != null){
                ExtlEvalVo extlEvalVo = new ExtlEvalVo();
                BeanUtils.copyProperties(extlEval,extlEvalVo);
                return Result.success(extlEvalVo);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.error(400,"系统繁忙，请稍后再试");
    }

    @PostMapping("/createAssessedEval")
    @ResponseBody
    @ApiOperation("新建或更新 最终评估")
    public Result createAssessedEval(@RequestBody AssessedEvalVo assessedEvalVo){

        try {

            Assert.hasLength(assessedEvalVo.getEvalProDetailsId(),"评估详情为空");
            Assert.hasLength(assessedEvalVo.getEvalProcessId(),"评估流程明细Id为空");
            Assert.hasLength(assessedEvalVo.getEvalProcessNo(),"流程编号为空");
            Assert.hasLength(assessedEvalVo.getAssessedVal().toString(),"我行确认价值为空");
            Assert.hasLength(assessedEvalVo.getAssessedCcyCd(),"币种为空");
            Assert.hasLength(assessedEvalVo.getAssessedEvalMtdCd().toString(),"确认方法为空");
            Assert.hasLength(assessedEvalVo.getAssessedDt().toString(),"确认日期为空");
            Assert.hasLength(assessedEvalVo.getConfMatureDt().toString(),"最终认定押品价值有效到期为空");

            evalProcessCltDetailsService.createAssessedEval(assessedEvalVo);
            return Result.success("操作成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.error(400,"系统繁忙，请稍后再试");
    }

    @GetMapping("/getAssessedEval")
    @ResponseBody
    @ApiOperation("获取最终评估")
    public Result getAssessedEval(@RequestParam("id") String id){
        try {
            EvalProcessCltDetails extlEval = evalProcessCltDetailsService.get(id);
            if(extlEval != null){
                AssessedEvalVo assessedEvalVo = new AssessedEvalVo();
                BeanUtils.copyProperties(extlEval,assessedEvalVo);
                return Result.success(assessedEvalVo);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.error(400,"系统繁忙，请稍后再试");
    }

    @GetMapping("/getEvalConfReason")
    @ResponseBody
    @ApiOperation("获取评估理由")
    public Result getEvalConfReason(@RequestParam("id") String id){
            try {
                EvalProcessCltDetails extlEval = evalProcessCltDetailsService.get(id);
                if(extlEval != null){
                    AmtConfReasonVo amtConfReasonVo = new AmtConfReasonVo();
                    amtConfReasonVo.setAmtConfReason(extlEval.getAmtConfReason());
                    amtConfReasonVo.setEvalProDetailsId(extlEval.getEvalProDetailsId());
                    EvalProcess evalProcess = evalProcessService.get(extlEval.getEvalProcessId());
                    amtConfReasonVo.setEvalProcessId(evalProcess.getEvalProcessId());
                    amtConfReasonVo.setProcessStsCd(evalProcess.getProcessStsCd());

                    return Result.success(amtConfReasonVo);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            return Result.error(400,"系统繁忙，请稍后再试");
        }
    @PostMapping("/updateEvalConfReason")
    @ResponseBody
    @ApiOperation("更新评估理由")
    public Result updateEvalConfReason(AmtConfReasonVo amtConfReasonVo){
            try {
                EvalProcessCltDetails extlEval = evalProcessCltDetailsService.get(amtConfReasonVo.getEvalProDetailsId());
                EvalProcess evalProcess = evalProcessService.get(extlEval.getEvalProcessId());
                extlEval.setAmtConfReason(amtConfReasonVo.getAmtConfReason());
                evalProcess.setProcessStsCd(amtConfReasonVo.getProcessStsCd());
                evalProcessCltDetailsService.updateSelective(extlEval);
                evalProcessService.updateSelective(evalProcess);
                return Result.success("操作成功");
            }catch (Exception e){
                    e.printStackTrace();
                }
                return Result.error(400,"系统繁忙，请稍后再试");
    }

}
